package DataAccessLayer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Pg数据库连接测试
 * @author tr
 */
public class PostgresHelper {



    LinkInfo link1 = new LinkInfo(
            "jdbc:postgresql://pgm-bp10yos72577yxn25o.pg.rds.aliyuncs.com:3433/hede",
            "hede",
            "Hipay2016"
    );
    LinkInfo link2 = new LinkInfo(
            "jdbc:postgresql://pgm-bp1esx39kuwc618r7o.pg.rds.aliyuncs.com:1921/postgres",
            "gdwd",
            "Hipay@2016"
    );


    public void Test() {

        //驱动包下载：https://jdbc.postgresql.org/download/  (注：下载的jar包，直接拷贝到项目文件夹下，项目内右键jar引用即可)


        try
        {
            LinkInfo link = link1;
            Connection conn = DriverManager.getConnection(link.getLink(), link.getUser(), link.getPassword());
            Statement stmt = conn.createStatement();
            String sql = "SELECT * FROM \"Person\"";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next())
            {
                int id = rs.getInt("ID");
                String name = rs.getString("Name");
                int age = rs.getInt("Age");
                System.out.println(id + " " + name + " " + age);
            }
        }
        catch (Exception ex)
        {
            System.out.println("出错了：" + ex.getMessage());
        }
    }

}
